package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.ShopListModel;

import java.util.List;


public class JingLiRightAdapter extends RecyclerView.Adapter<JingLiRightAdapter.ViewHolder> {
    List<ShopListModel.DataBean> myLiveList;
    Context mContext;
    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<ShopListModel.DataBean> myLiveList);
    }


    public JingLiRightAdapter(Context context, List<ShopListModel.DataBean> list) {
        this.mContext = context;
        this.myLiveList = list;

    }

    public void setDataList(List<ShopListModel.DataBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jingliright, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    int mycount = 0;

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        ShopListModel.DataBean myLive = myLiveList.get(holder.getAdapterPosition());


        holder.checkbox.setChecked(myLive.isChecked());

        holder.mTvTitle.setText(myLive.getCommodityName());
        holder.tv_num.setText(myLive.getApplyAmount() + "");
        holder.text_spec.setText(myLive.getSpecs());

        if (myLive.getCommodityCount() == 0) {
            holder.commodityCount.setVisibility(View.GONE);
        }
        if (myLive.getInventory() == 0) {
            holder.inventory.setVisibility(View.GONE);
        }

//        holder.commodityCount.setText("产品数量: " + myLive.getCommodityCount() + "");


//        holder.inventory.setText("库存数量: " + myLive.getInventory() + "");

        if (myLive.getCommodityCount() == 0 && myLive.getInventory() == 0) {
            holder.commodityCount.setVisibility(View.GONE);
            holder.inventory.setVisibility(View.GONE);
        }else if (myLive.getCommodityCount()!=0){
            holder.commodityCount.setVisibility(View.VISIBLE);
        }else if (myLive.getInventory()!=0){
            holder.inventory.setVisibility(View.VISIBLE);
        }

        holder.commodityCount.setText("在途数量: " + myLive.getCommodityCount() + "");


        holder.inventory.setText("库存数量: " + myLive.getInventory() + "");

        Glide.with(mContext).load(AppConfig.IMGURL + myLive.getPhoto()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);


        mycount = myLive.getApplyAmount();
        if (myLive.getMark().equals("")) {
            holder.text_beizhuxianshi.setText("备注: " + "暂无");
        } else {
            holder.text_beizhuxianshi.setText("备注: " + myLive.getMark());
        }

        if (myLive.getApplyAmount() == 0) {
            holder.img_add.setVisibility(View.VISIBLE);
            holder.layout_produce.setVisibility(View.GONE);
            holder.layout_guige.setVisibility(View.VISIBLE);
        } else {
            holder.layout_produce.setVisibility(View.VISIBLE);
            holder.img_add.setVisibility(View.GONE);
            holder.layout_guige.setVisibility(View.VISIBLE);
        }

        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int mycount = myLive.getApplyAmount() + 1;
                myLive.setApplyAmount(mycount);
                holder.tv_num.setText(myLive.getApplyAmount() + "");
                notifyItemChanged(position);
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });
        holder.img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int mycount = myLive.getApplyAmount() + 1;
                myLive.setApplyAmount(mycount);
                holder.tv_num.setText(myLive.getApplyAmount() + "");
                notifyItemChanged(position);
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });

        holder.tv_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int newcount = myLive.getApplyAmount();
                if (newcount == 0) {

                } else {
                    int mycount = myLive.getApplyAmount() - 1;
                    myLive.setApplyAmount(mycount);
                    holder.tv_num.setText(myLive.getApplyAmount() + "");
                    notifyItemChanged(position);
                }
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);

            }
        });
        holder.tv_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });

        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myLive.setChecked(!myLive.isChecked());
                holder.checkbox.setChecked(myLive.isChecked());

                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });

        holder.text_beizhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        if (myLive.getCollectionFlag().equals("0")) {
            holder.img_collect.setImageResource(R.mipmap.shoucang_no);
        } else {
            holder.img_collect.setImageResource(R.mipmap.shoucang_yes);
        }
        holder.img_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.layout_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
//        img_collect
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {


        TextView mTvTitle;

        ImageView ivAvatar;
        TextView tv_num;
        TextView tv_reduce;
        CheckBox checkbox;
        LinearLayout content;
        TextView tv_price;

        TextView tv_add;

        ImageView img_add;
        LinearLayout layout_produce;
        TextView text_beizhu;

        LinearLayout layout_guige;
        TextView text_beizhuxianshi;
        ImageView img_collect;
        LinearLayout layout_checkbox;
        TextView text_spec;
        TextView commodityCount;
        TextView inventory;

        LinearLayout layout_kuall;
        public ViewHolder(View itemView) {
            super(itemView);

            mTvTitle = (TextView) itemView.findViewById(R.id.tvCity);
            ivAvatar = (ImageView) itemView.findViewById(R.id.ivAvatar);
            tv_num = (TextView) itemView.findViewById(R.id.tv_num);
            tv_reduce = (TextView) itemView.findViewById(R.id.tv_reduce);
            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            content = (LinearLayout) itemView.findViewById(R.id.content);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);

            tv_add = (TextView) itemView.findViewById(R.id.tv_add);
            img_add = itemView.findViewById(R.id.img_add);
            layout_produce = itemView.findViewById(R.id.layout_produce);
            text_beizhu = itemView.findViewById(R.id.text_beizhu);
            layout_guige = itemView.findViewById(R.id.layout_guige);
            text_beizhuxianshi = itemView.findViewById(R.id.text_beizhuxianshi);
            img_collect = itemView.findViewById(R.id.img_collect);
            layout_checkbox = itemView.findViewById(R.id.layout_checkbox);
            text_spec = itemView.findViewById(R.id.text_spec);
            commodityCount = itemView.findViewById(R.id.commodityCount);
            inventory = itemView.findViewById(R.id.inventory);
        }
    }


}
